﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="Camera3D.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Render.
//
//   starLiGHT.Render is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Core is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Render. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Render are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 620                   $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2010-12-07 15:23:58 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.Camera.ThreeD
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    using starLiGHT.Collections;
    using starLiGHT.Scene.ThreeD;

    #endregion

    /// <summary>
    /// Basic camera class for 3D scenes.
    /// </summary>
    public class Camera3D : ICamera3D, ISceneObject3D
    {
        #region Member Variables
        private Viewport viewport;

        private Matrix projection;
        private bool projectionDirty;

        private Matrix view;
        private bool viewDirty;

        private Matrix viewProjection;
        private bool viewProjectionDirty;

        private Matrix inverseViewProjection;
        private bool inverseViewProjectionDirty;

        private BoundingFrustum frustum;
        private bool frustumDirty;

        private Vector3 position;
        private Vector3 target;

        private float nearClip = 0.01f;
        private float farClip = 1000.0f;

        private bool orthographic = false;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the Camera3D class.
        /// Use this for creating a camera that moves in 3-dimensional space.
        /// </summary>
        /// <param name="viewport">The Viewport to render with this camera.</param>
        public Camera3D(Viewport viewport)
            : this(viewport, new Vector3(-0.5f, 0.5f, -0.5f), Vector3.Zero)
        {
        }

        /// <summary>
        /// Initializes a new instance of the Camera3D class.
        /// Use this for creating a camera that moves in 3-dimensional space.
        /// </summary>
        /// <param name="viewport">The Viewport to render with this camera.</param>
        /// <param name="position">Position in 3-dimensional space of this camera.</param>
        /// <param name="target">The look-at-location of this camera.</param>
        public Camera3D(Viewport viewport, Vector3 position, Vector3 target)
        {
            this.viewport = viewport;
            this.position = position;
            this.target = target;

            this.projectionDirty = true;
            this.viewDirty = true;
            this.frustumDirty = true;
        }

        #endregion

        /// <summary>
        /// Gets or sets a value indicating whether the camera is or should be using an orhographic mode.
        /// </summary>
        public bool Orthographic
        {
            get
            {
                return this.orthographic;
            }

            set
            {
                if (this.orthographic != value)
                {
                    this.orthographic = value;
                    this.viewDirty = true;
                    this.projectionDirty = true;
                    this.frustumDirty = true;
                    this.viewProjectionDirty = true;
                    this.inverseViewProjectionDirty = true;
                }
            }
        }

        /// <summary>
        /// Gets or sets the position of the camera.
        /// </summary>
        public Vector3 Position
        {
            get 
            { 
                return this.position; 
            }

            set 
            {
                if (this.position != value)
                {
                    this.position = value;
                    this.viewDirty = true;
                    this.frustumDirty = true;
                    this.viewProjectionDirty = true;
                    this.inverseViewProjectionDirty = true;
                }
            }
        }

        /// <summary>
        /// Gets or sets the target or look at position of the camera.
        /// </summary>
        public Vector3 Target
        {
            get 
            {
                return this.target; 
            }

            set 
            {
                if (this.target != value)
                {
                    this.target = value;
                    this.viewDirty = true;
                    this.frustumDirty = true;
                    this.viewProjectionDirty = true;
                    this.inverseViewProjectionDirty = true;
                }
            }
        }

        /// <summary>
        /// Gets the projection matrix of the camera.
        /// </summary>
        public Matrix Projection
        {
            get 
            {
                if (this.projectionDirty)
                {
                    if (this.orthographic)
                    {
                        this.projection = Matrix.CreateOrthographic(4.0f * this.viewport.AspectRatio, 4.0f, this.nearClip, this.farClip);
                    }
                    else
                    {
                        this.projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, this.viewport.AspectRatio, this.nearClip, this.farClip);
                    }

                    this.projectionDirty = false;
                    this.viewProjectionDirty = true;
                    this.inverseViewProjectionDirty = true;
                    this.frustumDirty = true;
                }

                return this.projection; 
            }
        }

        /// <summary>
        /// Gets or sets the view matrix of the camera.
        /// Set accessor can only be used by classes that derive from Camera3D.
        /// </summary>
        public Matrix View
        {
            get 
            {
                if (this.viewDirty)
                {
                    this.view = Matrix.CreateLookAt(this.position, this.target, Vector3.Up);
                    this.viewDirty = false;
                    this.viewProjectionDirty = true;
                    this.inverseViewProjectionDirty = true;
                    this.frustumDirty = true;
                }

                return this.view; 
            }

            protected set
            {
                if (this.view != value)
                {
                    this.view = value;
                    this.viewDirty = false;
                    this.viewProjectionDirty = true;
                    this.inverseViewProjectionDirty = true;
                    this.frustumDirty = true;
                }
            }
        }

        /// <summary>
        /// Gets the view projection matrix of the camera.
        /// </summary>
        public Matrix ViewProjection
        {
            get
            {
                if (this.viewProjectionDirty)
                {
                    this.viewProjection = Matrix.Multiply(this.View, this.Projection); // IMPORTANT! Use the Properties NOT members for multiplication because of dirty flags
                    this.viewProjectionDirty = false;
                    this.inverseViewProjectionDirty = true;
                    this.frustumDirty = true;
                }

                return this.viewProjection;
            }
        }

        /// <summary>
        /// Gets the inverse matrix of the view projection matrix of the camera.
        /// </summary>
        public Matrix InverseViewProjection
        {
            get
            {
                if (this.inverseViewProjectionDirty)
                {
                    this.inverseViewProjection = Matrix.Invert(this.ViewProjection); // IMPORTANT! Use the Properties NOT members for multiplication because of dirty flags
                    this.inverseViewProjectionDirty = false;
                }

                return this.viewProjection;
            }
        }

        /// <summary>
        /// Gets the BoundingFrustum of the camera.
        /// </summary>
        public BoundingFrustum Frustum
        {
            get 
            {
                if (this.frustumDirty)
                {
                    this.frustum = new BoundingFrustum(this.ViewProjection); // IMPORTANT! Use the Properties NOT members for multiplication because of dirty flags
                    this.frustumDirty = false;
                }

                return this.frustum; 
            }
        }

        /// <summary>
        /// Gets or sets the Viewport of the camera.
        /// </summary>
        public Viewport Viewport
        {
            get 
            { 
                return this.viewport; 
            }

            set 
            { 
                this.viewport = value; 
            }
        }

        /// <summary>
        /// Gets or sets the distance of the near clipping plane of the camera.
        /// </summary>
        public float NearClip
        {
            get 
            { 
                return this.nearClip; 
            }

            set 
            {
                if (this.nearClip != value)
                {
                    this.nearClip = value;
                    this.projectionDirty = true;
                    this.viewProjectionDirty = true;
                    this.inverseViewProjectionDirty = true;
                    this.frustumDirty = true;
                }
            }
        }

        /// <summary>
        /// Gets or sets the distance of the far clipping plane of the camera.
        /// </summary>
        public float FarClip
        {
            get 
            { 
                return this.farClip; 
            }

            set
            {
                if (this.farClip != value)
                {
                    this.farClip = value;
                    this.projectionDirty = true;
                    this.viewProjectionDirty = true;
                    this.inverseViewProjectionDirty = true;
                    this.frustumDirty = true;
                }
            }
        }

        /// <summary>
        /// Obsolete function. Cameras don't draw themselves. At least not yet - maybe later in some kind of editor mode.
        /// </summary>
        /// <param name="core">The Core of the render engine.</param>
        /// <param name="renderer">The IRenderer responsible for drawing.</param>
        public void Draw(starLiGHT.Render.Core core, starLiGHT.Render.IRenderer renderer)
        {
            // nothing to draw, it's the camera
        }
    }
}
